﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/**
 * 自定义委托
 */
namespace P19_2 {
    //解释：
    //delegate：声明委托的关键字
    //double：目标方法的返回值类型
    //Calc(double x,double y)：名称为Cale的委托，并且接收有2个参数的目标方法
    public delegate double Calc(double x, double y);

    internal class Program {
        static void Main(string[] args) {

            Calculator calculator = new Calculator();
            Calc calc1 = new Calc(calculator.Add);
            Calc calc2 = new Calc(calculator.Sub);
            Calc calc3 = new Calc(calculator.Mul);
            Calc calc4 = new Calc(calculator.Div);

            double a = 100;
            double b = 200;
            double c = 0;
            c = calc1.Invoke(a, b); 
            Console.WriteLine(c);
            c = calc2.Invoke(a, b);
            Console.WriteLine(c);
            c = calc3(a, b);
            Console.WriteLine(c);
            c = calc4(a, b);
            Console.WriteLine(c);
            Console.ReadLine();
        }
    }

    class Calculator {

        public double Add(double x, double y) {
            return x + y;
        }

        public double Sub(double x, double y) {
            return x - y;
        }

        public double Mul(double x, double y) {
            return x * y;
        }

        public double Div(double x, double y) {
            return x / y;
        }   

    }
}
